package com.wxw.BaseLibrary.http;

import com.wxw.BaseLibrary.element.JsonObject;
import com.wxw.BaseLibrary.element.JsonProperty;
import com.wxw.BaseLibrary.infa.IGeneralEventCallback;

public abstract class DefaultHttpRequestProcessor implements IHttpRequestProcessor<RequestMessage> {
    protected IGeneralEventCallback eventCallback;

    @Override
    public void setHttpEventListener(IGeneralEventCallback listener) {
        eventCallback = listener;
    }

    @Override
    public void onStart(int what) {

    }

    @Override
    public void onFinish(int what) {

    }

    @Override
    public void onSucceed(int what, RequestMessage message) {
        triggerEvent(message, message.ResponseContent);
    }

    @Override
    public void onProgress(long bytesWritten, long totalSize) {

    }

    @Override
    public void onFailed(int what, String url, Exception ex, int StatusCode, long networkMillis) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setProperty("what", new JsonProperty(what));
        jsonObject.setProperty("url", new JsonProperty(url));
        jsonObject.setProperty("StatusCode", new JsonProperty(StatusCode));
        jsonObject.setProperty("networkMillis", new JsonProperty(networkMillis));
        triggerEvent(ex, jsonObject.toString());
    }

    protected void triggerEvent(Object message, String statusCode) {
        if (eventCallback != null) {
            eventCallback.finished(message, statusCode);
        }
    }
}
